import jfRequest, { upRequest } from "../../index";
import JFRequest from "@/service/request";
import { ILoginReq, ILoginResult, UploadResult } from "./types";
import { IResultType } from "types/request";
import { APPID, APPKEY, GET_ORGCODE_URL } from "@/service/request/config";
import LocalCache from "@/utils/cache";

export function accountLoginRequest(data: ILoginReq) {
  return jfRequest.post<IResultType<ILoginResult>>({
    url: "/api/v1/login",
    data,
  });
}
export function requestSysInfo(data?: any) {
  return jfRequest.get<any>({
    url: "/api/v1/getInfo",
    data,
    showLoading: false,
  });
}

export function requestUserMenusByRoleId(id: number) {
  return jfRequest.get<IResultType>({
    url: "/public/getMenuList",
    showLoading: false,
  });
}

// 获取token和orgCode
export async function getTokenAddOrgCode(
  appToken: String,
  timeStamp: String,
  accessToken: String
) {
  const appId = "JF-ISWCC-WEB";
  const jfRequest = new JFRequest({
    baseURL: GET_ORGCODE_URL,
    timeout: 30000,
    interceptors: {
      requestIntercepter: (config) => {
        config.headers.timeStamp = timeStamp;
        config.headers.appId = appId;
        config.headers.appToken = appToken;
        return config;
      },
      requestInterceptorCatch: (err) => {
        return err;
      },
      responseIntercepter: (config) => {
        return config;
      },
      responseInterceptorCatch: (err) => {
        return err;
      },
    },
  });
  const data = {
    appId: APPID,
    appKey: APPKEY,
    accessToken,
  };
  return jfRequest.post<IResultType<any>>({
    url: "/service/isptl/open/public/loginByAccessToken",
    showLoading: true,
    data: {
      data,
    },
  });
}

// 上传文件
export function uploadFile(headers: any, data: any) {
  return upRequest.post<UploadResult>({
    url: "/restful/files/uapi/upload/push",
    showLoading: true,
    headers,
    data,
  });
}

export function getClassIdMap(headers: any) {
  return upRequest.post<IResultType>({
    url: "/xgk/service/isbmp/public/getClassIdMapping",
    headers,
    data: {
      appId: APPID,
      appAlias: "JF-ISBM"
    },
    showLoading: false,
  });
}

// 获取筛选项配置
export function queryBaseconfig() {
  const orgCode = LocalCache.getCache("orgCode");
  return jfRequest.get<IResultType>({
    url: "/api/v1/patrol/base/queryBaseConfig",
    params: { orgCode: orgCode },
  });
}

export function queryTeacher(data?: any) {
  return jfRequest.post<IResultType<any>>({
    url: "/api/v1/patrol/role/queryTeacher",
    data,
  });
}

// 获取学年学期列表
export function getYearList() {
  return jfRequest.get<IResultType>({
    url: "/api/v1/common/getYearList",
  });
}

// 获取所有年级列表
export function getAllGrade() {
  return jfRequest.get<IResultType>({
    url: "/api/v1/common/getAllGrade",
  });
}

// 获取周期列表
export function getWeek(data: any) {
  return jfRequest.post<IResultType>({
    url: "/api/v1/common/getWeek",
    data,
  });
}
